package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.compete;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompete;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCompeteService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class ArchivedBu {
    @Autowired
    private ITCompeteService iTCompeteService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;

    //    更新归档状态，id为竞标表Id     0未归档 1已归档
    public SzResult updateCompeteArchived(String id, String archived, String token, String plantId) throws Exception {
        if (archived != null) {
            QueryWrapper<TCompete> QueryWrapper = new QueryWrapper<>();
            QueryWrapper.eq("id", id);
            QueryWrapper.eq("DATA_FROM", plantId);
            TCompete Tarchived = new TCompete();
            Tarchived.setArchived(Integer.valueOf(archived));
            iTCompeteService.update(Tarchived, QueryWrapper);
        } else {
            return SzResult.error("archived不能为空");
        }
//获取竞标表的标题
        TCompete tCompete = iTCompeteService.getById(id);
        String title = tCompete.getTitle();
        SysUser user1 = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("更新归档状态", user1.getTrueName(), "更新了" + title + "的归档状态", plantId);
        return SzResult.Success();
    }

    //    一键归档功能，归档已经完成最后审批的竞价数据STATUS大于51，且是一个月以前发布的数据CREATE_DATE，默认为31天之前，根据
    public SzResult updateCompeteArchivedAll(String token, String plantId) throws Exception {
        QueryWrapper<TCompete> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_FROM", plantId);
//        STATUS大于等于51
        queryWrapper.ge("STATUS", 51);
        queryWrapper.lt("CREATE_DATE", java.time.LocalDateTime.now().minusDays(31));
        TCompete tCompete = new TCompete();
        tCompete.setArchived(1);
        iTCompeteService.update(tCompete, queryWrapper);
        return  SzResult.Success();
    }
}
